<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="16" style="margin-right:10px;">
            <el-form-item prop="code">
              <el-input
                prefix-icon="el-icon-key"
                v-model="ruleForm.code"
                placeholder="请输入验证码"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <img
              :src="getCodeUrl"
              alt=""
              style="width:100%;height:40px;"
              @click="getCode"
            />
          </el-col>
        </el-row>
        <el-form-item prop="checked">
          <el-checkbox v-model="ruleForm.checked"></el-checkbox>
          我已阅读并同意<el-link type="primary">用户协议</el-link>和<el-link
            type="primary"
            >隐私条款</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="register"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <!-- 使用子组件 -->
    <register ref="register"></register>
  </div>
</template>
<script>
// 按需导入
import { setToken } from '@/utils/token'
// 导入注册子组件
import register from '../register/register'
export default {
  data () {
    return {
      getCodeUrl: `${process.env.VUE_APP_BASEURL}/captcha?type=login`,
      ruleForm: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: false
      },
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) return callback(new Error('手机号不能为空！'))
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) return callback(new Error('手机号不合法！'))
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度大于6小于16', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空！', trigger: 'blur' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议！'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  // 注册子组件
  components: {
    register
  },
  methods: {
    getCode () {
      this.getCodeUrl = `${
        process.env.VUE_APP_BASEURL
      }/captcha?type=login&t=${new Date() - 0}`
    },
    login () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        // this.$axios.post('/login', this.ruleForm).then(res => {
        //   console.log(res)
        //   if (res.code == 200) {
        //     this.$message({
        //       message: '登录成功！',
        //       type: 'success'
        //     })
        //     localStorage.setItem('heimamm64', res.data.token)
        //     this.$router.push('/heihei')
        //   } else {
        //     this.$message.error(res.message)
        //     this.getCode()
        //   }
        // })
        const res = await this.$axios.post('/login', this.ruleForm)
        if (!valid) return
        if (res.code == 200) {
          this.$message({
            message: '登录成功！',
            type: 'success'
          })
          setToken(res.data.token)
          // localStorage.setItem('heimamm64', res.data.token)
          this.$router.push('/layout')
        } else {
          this.$message.error(res.message)
          this.getCode()
        }
      })
    },
    register () {
      this.$refs.register.dialogVisible = true
    }
  }
}
</script>
<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);

  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      margin-bottom: 29px;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
  }
}
</style>
